package managerEngine

import (
	"fmt"

	"gitee.com/RocsSun/report/config"
	"gitee.com/RocsSun/report/models"
	"github.com/go-resty/resty/v2"
)

type OpManagerClient struct {
	// Params  StructToMap
	Spider ManagerEngineSpider
	urls   models.OpManagerURIs
	params OpManagerQueryParama
}

func NewOpManagerClient() *OpManagerClient {
	op := &OpManagerEngineSpider{}
	op.UnSerializerCookies(config.ConfigContext.ManagerEngine.OpManager.CookiesPath)
	return &OpManagerClient{
		Spider: op,
		urls:   config.ConfigContext.ManagerEngine.OpManager.URI,
		params: DailyReportQueryParam{},
	}
}

func (o *OpManagerClient) BaseData(params *map[string]string) (*resty.Response, error) {
	url := o.urls.BaseURL + o.urls.GraphData
	return o.Spider.GetNetWorkData(url, params)
}

// DeviceMonitors 获取设备的监视清单
func (o *OpManagerClient) DeviceMonitors(ip string) *resty.Response {
	url := o.urls.BaseURL + o.urls.DeviceAssociatedMonitors + ip
	DevicesMonitorResponse, _ := Spider.R().Get(url)
	return DevicesMonitorResponse
}

// YesterdayMonitorCpuData 获取昨天的CPU监控数据。生成报告。
func (o *OpManagerClient) YesterdayMonitorCpuData(ip string) *resty.Response {
	res, _ := o.BaseData(o.params.CPUQueryParam(ip))
	return res
}

// YesterdayMonitorDiskData 获取昨天的CPU监控数据。生成报告。
func (o *OpManagerClient) YesterdayMonitorDiskData(ip string) []*resty.Response {
	// var result []*resty.Response = make([]*resty.Response, 6)
	var result []*resty.Response
	for _, v := range o.params.DiskQueryParam(ip) {
		resp, _ := o.BaseData(v)

		result = append(result, resp)
	}
	return result
}

// YesterdayMonitorDiskData 获取昨天的CPU监控数据。生成报告。
func (o *OpManagerClient) YesterdayMonitorMemData(ip string) *resty.Response {
	res, _ := o.BaseData(o.params.MemQueryParam(ip))
	return res
}

// YesterdayMonitorDiskData 获取昨天的CPU监控数据。生成报告。
func (o *OpManagerClient) YesterdayMonitorProcessData(ip string) map[string]*resty.Response {
	var result map[string]*resty.Response = make(map[string]*resty.Response)
	var url string = o.urls.BaseURL + o.urls.ProcessDetail
	for processName, v := range o.params.ProcessQueryParam(ip) {
		if v != nil {
			resp, _ := o.Spider.GetNetWorkData(url, v)
			result[processName] = resp
		}
	}
	return result
}

func (o *OpManagerClient) YesterdayMonitorServicData(ip string) map[string]*resty.Response {
	// var result []*resty.Response
	var result map[string]*resty.Response = make(map[string]*resty.Response)
	var url string = o.urls.BaseURL + o.urls.ServiceDetail
	for _, v := range o.params.ServiceQueryParam(ip) {

		if v != nil {
			tmp := *v
			serviceName := tmp["Instance"]
			// 处理服务重名问题和无服务数据问题
			for i := 1; i < 20; i++ {
				// testResult 存在了服务名。添加新的服务名。
				if _, ok := result[serviceName]; ok {
					serviceName = serviceName + fmt.Sprint(i)
				} else {
					break
				}
			}

			resp, _ := o.Spider.GetNetWorkData(url, v)
			result[serviceName] = resp
			// result = append(result, resp)
		}
	}
	return result
}

func (o *OpManagerClient) YesterdayMonitorDataServiceParams(ip string) []*resty.Response {
	var result []*resty.Response

	for _, v := range o.params.ServiceQueryParam(ip) {
		resp, _ := o.BaseData(v)
		result = append(result, resp)
	}
	return result
}

func (o *OpManagerClient) DevicesMemUnit(ip string) *resty.Response {
	url := o.urls.BaseURL + o.urls.DeviceSummary
	resp, _ := o.Spider.GetNetWorkData(url, o.params.DeviceSummaryQueryParam(ip))
	return resp
}
